﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MagicArena.framework.state
{
    public class State
    {
        #region Enumerations for common states
        public enum Common
        {
            Xpos=0, Ypos=1, Width=2, Height=3,Health=4,Energy=5, Radius=6, TargetDist=7
        }
        #endregion

        #region Properties
        public string Name
        {
            get
            {
                return Id.ToString();
            }
        }
        public float Value
        {
            get;
            set;
        }
        public Common Id
        {
            get;
            private set;
        }
        #endregion

        public State(Common id, float value)
        {
            Id = id;
            Value = value;
        }

        /// <summary>
        /// Checks to see if obj is equal to this State
        /// </summary>
        /// <param name="obj">Object to check equality with</param>
        /// <returns>true if obj is of type State and has the same Name as this State, false otherwise</returns>
        public override bool Equals(object obj)
        {
            if (obj != null && obj is State)
            {

                if (Id ==(((State)obj).Id) && Value == ((State)obj).Value)
                    return true;
                
            }
            return false;
        }

        public override string ToString()
        {
            return string.Format("State[{0}, {1}]", Name, Value);
        }


        #region Public static helper methods
        public static State FindById(State[] arr, Common Id)
        {
            return Array.Find<State>(arr, delegate(State s)
            {
                if (s.Id == Id) return true;
                return false;
            });
        }
        public static State FindState(State[] arr, State.Common Id, float defaultVal = 0)
        {
            State s = State.FindById(arr, Id);
            if (s == null) s = new State(Id, defaultVal);
            return s;
        }
        /// <summary>
        /// Searches for a state which modifies by at least stateModified
        /// </summary>
        /// <param name="arr">array of States to search</param>
        /// <param name="stateModified">State to check against</param>
        /// <returns>A State with same Id as stateModified and who value is more negative or more positive than stateModified's value. If none can be found returns null</returns>
        public static State FindState(State[] arr, State stateModified)
        {
            return Array.Find<State>(arr, delegate(State s)
            {
                bool rtn=false;
                if (s.Id == stateModified.Id)
                {
                    rtn = 
                        stateModified.Value < 0 
                        ? s.Value < stateModified.Value 
                        : s.Value > stateModified.Value;
                }
                return rtn;
            });
        }
        #endregion

        #region Private helper methods

        #endregion
    }
}
